package org.lock;

import org.I0Itec.zkclient.ZkClient;
import org.junit.Test;

public class ZkLock1 {


    @Test
    public void lock() throws Exception {
        ZkClient zkClient = new ZkClient("10.10.10.92:2181");

        final boolean exists = zkClient.exists("/mylock");

        if (!exists) {

            try {
                //临时节点，客户端关闭就删除
                zkClient.createEphemeral("/mylock", "测试分布式锁");
                System.err.println("获取锁");
                System.err.println("处理业务");
            } catch (Exception e) {
                System.err.println("获取锁失败");
            }
        } else {
            System.out.println("等待释放");
        }

    }

}
